package v1

import (
	"ginTea/model"
	"ginTea/utils/errmsg"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

// 添加知识点
func AddTips(c *gin.Context) {
	var code int
	var data model.Tips
	_ = c.ShouldBindJSON(&data)
	code = model.CreateTips(&data)
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"success": true,
	})
}

// 删除知识点
func DeleteTips(c *gin.Context) {
	var code int
	id, _ := strconv.Atoi(c.Param("id"))
	code = model.DeleteTips(id)
	if code == errmsg.ERROR {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": false,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code":    code,
			"msg":     errmsg.GetErrMsg(code),
			"success": true,
		})
	}
}

// 获取知识点列表
func GetTipsList(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	page, _ := strconv.Atoi(c.Query("page"))
	keyword := c.Query("keyword")
	if pageSize == 0 {
		pageSize = 10
	}
	if page == 0 {
		page = 1
	}
	data := model.GetTipsList(page, pageSize, keyword)
	c.JSON(http.StatusOK, data)
}

// 获取知识点详情
func GetTipsInfo(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	data, code := model.GetTipsInfo(id)
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"data":    data[0],
		"msg":     errmsg.GetErrMsg(code),
		"success": true,
	})
}

// 编辑知识点
func UpdateTips(c *gin.Context) {
	var code int
	var data model.Tips
	id, _ := strconv.Atoi(c.Param("id"))
	c.ShouldBindJSON(&data)
	code = model.UpdateTips(id, &data)
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"success": true,
	})
}
